package cn.sfa.controller.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.DateUtil;
import cn.sfa.common.utils.TableNameEnum;
import cn.sfa.entity.affixItem.SaleAffixItem;
import cn.sfa.entity.customer.SaleCustomerSee;
import cn.sfa.entity.customer.SaleHideCustomer;
import cn.sfa.entity.prospectus.SaleProspectus;
import cn.sfa.entity.system.BranchSysCode;
import cn.sfa.entity.system.ProviderSysCode;
import cn.sfa.entity.users.SaleUsersLogin;
import cn.sfa.service.customer.CustomerSeeService;
import cn.sfa.service.customer.CustomerService;
import cn.sfa.service.login.LoginService;
/**
 * 
* 类名称：CustomerSeeController  
* 类描述： 客户拜访首页管理类
* 创建人：lenovo   
* 创建时间：2012-2-20 下午8:50:06   
* 修改人：lenovo   
* 修改时间：2012-2-20 下午8:50:06   
* 修改备注：   
* @version
 */
@Controller
@RequestMapping("/customer/manage/see/index")
public class CustomerSeeController {

	Logger log = LoggerFactory.getLogger(CustomerSeeController.class);
	@Autowired
	CustomerSeeService customerseeService;
	@Autowired
	LoginService loginService;
	@Autowired
	CustomerService customerService;
	private Pagination pagination;
	
	
	/**
	 * 客户关联
	 * TODO
	 * @author: LiXiang
	 * @param productCode
	 * @param productTitle
	 * @param productProvider
	 * @param currentPage
	 * @param request
	 * @param map
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("selectcustomer")
	public String saleProductForPlan(
			@RequestParam(value = "currentPage", required = false) String currentPage,// 页码
			HttpServletRequest request, ModelMap map,
			HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("utf-8");
		int pageIndex;
		try {
			pageIndex = Integer.valueOf(currentPage);
		} catch (Exception ex) {
			pageIndex = 1;
		}
		String url = request.getContextPath()
				+ "/customer/manage/see/index/selectcustomer";

		Pagination pagination = customerService.RelevanceSaleProduct(pageIndex,
				10, url);

		map.put("pagination", pagination);
		return "customer/customersee/customers";
	}
	
	
	@RequestMapping("customers")
	public String getFollow(
			@RequestParam(value = "currentPage", required = false) String currentPage,// 页码
			@RequestParam(value = "userName", required = false) String userName,// 用户名称
			@RequestParam(value = "customerName", required = false) String customerName,// 客户名称
			HttpServletRequest request, ModelMap map,
			HttpServletResponse response,
			@RequestParam(value = "beginCreateDate", required = false) String beginCreateDate,
			@RequestParam(value = "endCreateDate", required = false) String endCreateDate) {
		int pageIndex;
		try {
			pageIndex = Integer.valueOf(currentPage);
		} catch (Exception ex) {
			pageIndex = 1;
		}
		Pagination pagination=null;
		pagination = customerService.searchCustomersFollow(pageIndex, 10, request
				.getContextPath()
				+ "/customer/manage/see/index/getfollow?"
				+ "userName="
				+ CommonUtil.jugeAndFixNull(userName)
				+ "&customerName="
				+ CommonUtil.jugeAndFixNull(customerName)
				+ "&beginCreateDate="
				+ (CommonUtil.isEmpty(beginCreateDate) ? "" :  beginCreateDate)
				+ "&endCreateDate="
				+ (CommonUtil.isEmpty(endCreateDate) ? "" :  endCreateDate), userName,customerName,
				beginCreateDate,  endCreateDate);
		map.put("pagination", pagination);
		map.put("customerName", customerName);
		map.put("userName", userName);
		map.put("beginCreateDate", CommonUtil.isEmpty(beginCreateDate) ? ""
				:  beginCreateDate);
		map.put("endCreateDate", CommonUtil.isEmpty(endCreateDate) ? ""
				:  endCreateDate);
		map.put("page", pageIndex);

		return "customer/customersee/index";
	}

	/**
	 * 
	 * @param request
	 * @param map
	 * @param response
	 * @return
	 */
	@RequestMapping("/add")
	public String add(HttpServletRequest request, ModelMap map,
			HttpServletResponse response) {
		SaleUsersLogin user = (SaleUsersLogin) request.getSession()
				.getAttribute("user");
		String userName = user.getUserName();
		map.put("userName", userName);
		return "customer/customersee/add";

	}

	/**
	 * AJAX新增跟进
	 * 
	 * @param affixStr
	 * @param vildstartdate
	 * @param vildstopdate
	 * @param provider
	 * @param request
	 * @param model
	 * @param response
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/insert")
	public void insert(
			SaleCustomerSee saleCustomerSee,
			HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		response.setContentType("text/json");
		SaleUsersLogin user = (SaleUsersLogin) request.getSession()
				.getAttribute("user");
		PrintWriter out = response.getWriter();
		
		String visitTime = request.getParameter("visitTime");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date visitDate = null;
		try {
			 visitDate = format.parse(visitTime);
		} catch (Exception e) {
		}
		
		saleCustomerSee.setVisitDate(visitDate);
		saleCustomerSee.setUserId(user.getSaleUsersLoginId());
		
		JSONObject jo = new JSONObject();
		try {
			boolean result = customerService.insertCustomerSee(saleCustomerSee); 
			if (result) {
				jo.put("result", true);
			} else {
				jo.put("result", false);
			}
		} catch (Exception e) {
			jo.put("result", false);
		}
		out.print(jo);
		out.flush();
		out.close();
	}

	/**
	 * 修改跟进
	 * 
	 * @param request
	 * @param map
	 * @param response
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public String edit(
			@PathVariable("id") String id,
			HttpServletRequest request, ModelMap map,
			HttpServletResponse response) {
		SaleCustomerSee saleCustomerSee = customerService
				.getSaleCustomerSeeByID(id);
		String customerName = customerService.getCustomer(saleCustomerSee.getCustomerId()).getCustomerName();
		String userName = loginService.getUserById(saleCustomerSee.getUserId()).getUserName();
		map.put("id", id);
		map.put("saleCustomerSee", saleCustomerSee);
		map.put("customerName", customerName);
		map.put("userName", userName);
		return "customer/customersee/edit";
	}
	/**
	 * AJAX修改
	 * @param saleproviderplanbookid
	 * @param affixStr
	 * @param vildstartdate
	 * @param vildstopdate
	 * @param provider
	 * @param request
	 * @param model
	 * @param response
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/update")
	public void update(
			@RequestParam(value = "followDate", required = false) String followDate,
			HttpServletRequest request, SaleCustomerSee model,
			HttpServletResponse response, ModelMap map) throws Exception {
		response.setContentType("text/json");
		PrintWriter out = response.getWriter();
		
		String visitTime = request.getParameter("visitTime");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date visitDate = null;
		try {
			 visitDate = format.parse(visitTime);
		} catch (Exception e) {
		}
		
		model.setVisitDate(visitDate);
		
		SaleUsersLogin user = (SaleUsersLogin) request.getSession()
				.getAttribute("user");
		model.setUpdateDate(new Date());
		model.setUpdateUser(user.getSaleUsersLoginId());
		model.setVisitDate(visitDate);
		
		JSONObject jo = new JSONObject();
		try {
			boolean result = customerService.updateCustomerFollow(model); 
			if (result) {
				jo.put("result", true);
			} else {
				jo.put("result", false);
			}
		} catch (Exception e) {
			jo.put("result", false);
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 删除计划书
	 * @param id
	 * @param request
	 * @param response
	 * @param map
	 * @throws Exception
	 */
	@RequestMapping("/del/{id}")
	public void delete(@PathVariable("id") String id,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		response.setContentType("text/json");
		PrintWriter out = response.getWriter();
		JSONObject jo = new JSONObject();
		boolean result;
		try {
			result = customerService.deleteCustomerFollowById(id) ? true : false;
			jo.put("result", result);
		} catch (Exception ex) {
			jo.put("result", false);
		}
		out.print(jo);
		out.flush();
		out.close();
	}
//	
//	/**
//	 * 计划书信息
//	 * @param saleproviderplanbookid
//	 * @param request
//	 * @param map
//	 * @param response
//	 * @return
//	 */
//	@RequestMapping("/info/{saleproviderplanbookid}")
//	public String info(	
//			@PathVariable("saleproviderplanbookid") String saleproviderplanbookid,
//			HttpServletRequest request, ModelMap map,
//			HttpServletResponse response){
//		SaleProspectus prospectus = prospectusService.getPlanBookByID(saleproviderplanbookid);
//		List<SaleAffixItem> affixList = affixService.getAffix("SALEPROVIDERPLANBOOK", saleproviderplanbookid);
//		map.put("affixList", affixList);
//		map.put("prospectus", prospectus);
//		return "customer/customersee/info";
//	}
	
}
